package com.llc.mall.user.application.query.service.impl;

import com.llc.mall.common.cache.constant.CacheNames;
import com.llc.mall.user.application.query.service.AreaQueryService;
import com.llc.mall.user.infrastructure.persist.mapper.AreaMapper;
import com.llc.mall.user.interfaces.vo.AreaVo;
import com.mall4j.cloud.api.user.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AreaQueryServiceImpl implements AreaQueryService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    @Cacheable(cacheNames = CacheNames.AREA_INFO_KEY, key = "'areaList'", sync = true)
    public List<AreaVo> getAreaListInfo() {
        List<AreaVo> areaList = areaMapper.getAreaListInfo();
//        for (AreaVo province:areaList){
//            List<Long> cityAll = new ArrayList<>();
//            for (AreaVO city:province.getAreas()){
//                cityAll.add(city.getAreaId());
//                List<Long> areaAll = new ArrayList<>();
//                for (AreaVO area:city.getAreas()){
//                    areaAll.add(area.getAreaId());
//                }
//                city.setAreaIds(areaAll);
//            }
//            province.setAreaIds(cityAll);
//        }
        return areaList;
    }

    @Override
    @Cacheable(cacheNames = CacheNames.AREA_KEY, key = "'list:' + #pid")
    public List<AreaVo> listByPid(Long pid) {
        return areaMapper.listByPid(pid);
    }
}
